{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "from configure.settings import DBSelector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = DBSelector().mongo('qq')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "doc = db['db_uqer']['bond_daily']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "path='../data/优矿最后一天拔下来/dialy_lohc.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "336358"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 336358 entries, 0 to 336357\n",
      "Data columns (total 20 columns):\n",
      " #   Column           Non-Null Count   Dtype  \n",
      "---  ------           --------------   -----  \n",
      " 0   Unnamed: 0       336358 non-null  int64  \n",
      " 1   secID            336358 non-null  object \n",
      " 2   ticker           336358 non-null  int64  \n",
      " 3   secShortName     336358 non-null  object \n",
      " 4   exchangeCD       336358 non-null  object \n",
      " 5   tradeDate        336358 non-null  object \n",
      " 6   preClosePrice    336358 non-null  float64\n",
      " 7   openPrice        336358 non-null  float64\n",
      " 8   highestPrice     336358 non-null  float64\n",
      " 9   lowestPrice      336358 non-null  float64\n",
      " 10  closePrice       336358 non-null  float64\n",
      " 11  turnoverVol      336358 non-null  int64  \n",
      " 12  turnoverValue    336358 non-null  float64\n",
      " 13  dealAmount       336358 non-null  int64  \n",
      " 14  accrInterest     336310 non-null  float64\n",
      " 15  YTM              57683 non-null   float64\n",
      " 16  VWAP             336358 non-null  float64\n",
      " 17  grossClosePrice  336358 non-null  float64\n",
      " 18  netClosePrice    336358 non-null  float64\n",
      " 19  isOpen           336358 non-null  int64  \n",
      "dtypes: float64(11), int64(5), object(4)\n",
      "memory usage: 51.3+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "del df['Unnamed: 0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "json_data = df.to_json(orient='records',force_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "js=json.loads(json_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'secID': '110030.XSHG',\n",
       " 'ticker': 110030,\n",
       " 'secShortName': '格力转债',\n",
       " 'exchangeCD': 'XSHG',\n",
       " 'tradeDate': '2017-01-03',\n",
       " 'preClosePrice': 113.83,\n",
       " 'openPrice': 113.8,\n",
       " 'highestPrice': 114.15,\n",
       " 'lowestPrice': 113.8,\n",
       " 'closePrice': 114.04,\n",
       " 'turnoverVol': 3650,\n",
       " 'turnoverValue': 4163418.0,\n",
       " 'dealAmount': 0,\n",
       " 'accrInterest': 0.027397,\n",
       " 'YTM': None,\n",
       " 'VWAP': 114.066,\n",
       " 'grossClosePrice': 114.04,\n",
       " 'netClosePrice': 114.012603,\n",
       " 'isOpen': 1}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "js[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch=5000 # 每次1000\n",
    "count = int(len(js)/batch+1)\n",
    "\n",
    "for i in range(count):\n",
    "\n",
    "    doc.insert_many(js[batch*i:batch*(i+1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "336358"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(js)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "906064a35c7980fda78a8b97014bc5560dca56efd2e047844bdf4fb8a9b9c8a3"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('cpy')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
